
package uoc.biblioteca.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import uoc.biblioteca.mappings.object.UserVo;

public class AdquisicioAction extends DispatchAction {
	protected String NO_USER_SESSION = "Ho sentim, per&ograve; la teva sessi&oacute; ha caducat.";
   
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward menuLlibre(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
    	 //Comprobar si la session esta guardada
		UserVo userLogin = obtenerUserSession(request);
		if(userLogin==null){
			request.getSession().setAttribute("ERROR","\n" +NO_USER_SESSION);
			return mapping.findForward("errorNotUser");
		}
    	
    	return mapping.findForward("menuLlibre");
    }

    
    public ActionForward menuRevista(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	
    	//Comprobar si la session esta guardada
		UserVo userLogin = obtenerUserSession(request);
		if(userLogin==null){
			request.getSession().setAttribute("ERROR","\n" +NO_USER_SESSION);
			return mapping.findForward("errorNotUser");
		}
    	
    	
    	return mapping.findForward("menuRevista");
    }
    
    
	public UserVo obtenerUserSession(HttpServletRequest request){
		
		UserVo user = null;
		
		try{
			user = (UserVo) request.getSession().getAttribute("userLogin");
		}
		
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return user;
		
	}
    
}